package com.example.shopping_li.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.example.shopping_li.R;

public class BuyDialog extends Dialog implements View.OnClickListener {
    ImageView iv;
    AmountView amountView;
    Button ensureBtn;
    public interface OnEnsureListener{
       // public void onEnsure(int count);
       public void onEnsure(int msg);//填进去的数据
    }

    OnEnsureListener onEnsureListener;
    public void setOnEnsureListener(OnEnsureListener onEnsureListener){
        this.onEnsureListener = onEnsureListener;
    }
//    public  interface  OnEnsureListener{

//    }
//    EditUserDialog.OnEnsureListener onEnsureListener;//声明接口的变量
//    public void setOnEnsureListener(EditUserDialog.OnEnsureListener onEnsureListener) {
//        this.onEnsureListener = onEnsureListener;
//    }
    public BuyDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_amount);
        iv=findViewById(R.id.dialog_amount_iv);
        amountView=findViewById(R.id.dialog_amountview);
        ensureBtn=findViewById(R.id.dialog_amount_btn);
        iv.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_amount_iv:
                cancel();
            break;
            case R.id.dialog_amount_btn:
                if (onEnsureListener!=null) {
                    int count=amountView.getAmountCount();
                    onEnsureListener.onEnsure(count);
                }
                break;
        }
    }
    public void setDialogWidth() {
        Window window = getWindow();//当前屏幕窗口对象
        WindowManager.LayoutParams wlp = window.getAttributes();//获取窗口信息参数
        //获取屏幕宽度
        Display d = window.getWindowManager().getDefaultDisplay();
        wlp.width = (int) d.getWidth();//对话框窗口宽度为屏幕窗口宽度
        wlp.gravity = Gravity.BOTTOM;//从底部弹出对话框
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(wlp);
    }
}
